#include "./drain_task.h"
#include "../driver/sensor.h"
#include "../driver/ctrl.h"


namespace task {

DrainTask drainTask;

using namespace driver;

constexpr uint32_t DELAY_MS = 30 * 1000;

void DrainTask::run() {

    for (;;) {
        if (sensor.is_leak1()) {
            on_drain();
        } 
        osDelay(1000);
    }
}

void DrainTask::on_drain() {
    ctrl.open(parts::DRAIN_PUMP);
    while (true) {
        if (!sensor.is_leak1()) {
            break;
        }
        osDelay(200);
    }
    osDelay(DELAY_MS);
    ctrl.close(parts::DRAIN_PUMP);
}


}
